﻿using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace Fluffy.Archive.Behaviors
{
	public class BindableSelectedItemsBehavior : Behavior<DataGrid>
	{
		public IList SelectedItems
		{
			get { return (IList)GetValue(SelectedItemsProperty); }
			set { SetValue(SelectedItemsProperty, value); }
		}

		public static readonly DependencyProperty SelectedItemsProperty =
			DependencyProperty.Register("SelectedItems", typeof(IList), typeof(BindableSelectedItemsBehavior), new UIPropertyMetadata(null));
		
		protected override void OnAttached()
		{
			base.OnAttached();

			AssociatedObject.SelectionChanged += OnSelectedItemsChanged;
		}

		protected override void OnDetaching()
		{
			base.OnDetaching();

			AssociatedObject.SelectionChanged -= OnSelectedItemsChanged;
		}

		private void OnSelectedItemsChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
		{
			SelectedItems = AssociatedObject.SelectedItems;
		}
	}
}
